﻿using Opc.Ua;
using Opc.Ua.Client;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Entity.ScadaBaseInfo;
using SHRIS.MIS.Entity.ScadaData;
using SHRIS.MIS.Service.OpcService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SHRIS.MIS.Service.Device
{
    public class TrackRobot_08 : BasicDevice
    {
        private readonly IEnumerable<OpcDataSourceEntity> _opcDataSourceEntities;
        public TrackRobot_08(DeviceEntity deviceEntity,
           WorkshopCommunicationService workshopCommunicationService,
           IEnumerable<OpcDataSourceEntity> opcDataSourceEntities)
           : base(deviceEntity, workshopCommunicationService)
        {
            _opcDataSourceEntities = opcDataSourceEntities;
            CheckOutDataType();
        }

        private void CheckOutDataType()
        {
            //对所有OPC 数据类型进行校验，如果不对记入LOG

        }

        public override void InitalDevice()
        {
            //创建opc 订阅

            //找出该通道下的订阅数据，dataAction==4 为订阅
            var subscriptionDatas = _opcDataSourceEntities.Where(i => i.DataAction == 4);

            //找出订阅数据的服务器列表
            var serverIds = subscriptionDatas.Select(i => i.ServerId).Distinct().ToList();

            if (serverIds.Count() == 1)
            {
                _workshopCommunicationService.CreateOpcSubscription(subscriptionDatas, DataHandler);
            }


            //用deviceCode作为订阅组名字
            //var subscriptionName = _deviceEntity.DeviceCode;

        }

        private void DataHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                //Console.WriteLine(notification.Value.StatusCode.ToString());

                if (notification != null)
                {
                    Guid dataId = new Guid();
                    bool isId = Guid.TryParse(monitoredItem.DisplayName, out dataId);
                    if (isId && (!dataId.IsGuidNullOrEmpty()))
                    {
                        var dataSource = _opcDataSourceEntities.Where(i => i.Id == dataId).First();
                        switch (dataSource.DataName)
                        {
                            case "AtStation":
                                {
                                    AtStation = (bool)notification.Value.Value;
                                    break;
                                }
                            case "Full":
                                {
                                    Full = (bool)notification.Value.Value;
                                    break;
                                }
                            case "WorkpieceNum":
                                {
                                    WorkpieceNum = (short)notification.Value.Value;
                                    break;
                                }
                            case "Empty":
                                {
                                    Empty = (bool)notification.Value.Value;
                                    break;
                                }
                            case "MaterialCode":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        MaterialCode = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            case "MaterialCode1":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        MaterialCode1 = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            case "Reason1":
                                {
                                    Reason1 = (short)notification.Value.Value;
                                    break;
                                }
                            case "MaterialCode2":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        MaterialCode2 = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            case "Reason2":
                                {
                                    Reason2 = (short)notification.Value.Value;
                                    break;
                                }
                            case "MaterialCode3":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        MaterialCode3 = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            case "Reason3":
                                {
                                    Reason3 = (short)notification.Value.Value;
                                    break;
                                }
                            case "MaterialCode4":
                                {
                                    if (notification.Value.Value != null)
                                    {
                                        MaterialCode4 = notification.Value.Value.ToString();
                                    }
                                    break;
                                }
                            case "Reason4":
                                {
                                    Reason4 = (short)notification.Value.Value;
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 设备参数

        private string materialCode1;
        /// <summary>
        /// 物料码1
        /// NG车独有
        /// </summary>
        public string MaterialCode1
        {
            get
            {
                return materialCode1;
            }
            set
            {
                string previewVlaue = materialCode1;
                materialCode1 = value;
                OnPropertyChanged("MaterialCode1", previewVlaue, materialCode1);
            }
        }

        private short reason1;
        /// <summary>
        /// 物料1NG原因
        /// </summary>
        public short Reason1
        {
            get
            {
                return reason1;
            }
            set
            {
                short previewVlaue = reason1;
                reason1 = value;
                OnPropertyChanged("Reason1", previewVlaue, reason1);
            }
        }

        private string materialCode2;
        /// <summary>
        /// 物料码2
        /// NG车独有
        /// </summary>
        public string MaterialCode2
        {
            get
            {
                return materialCode2;
            }
            set
            {
                string previewVlaue = materialCode2;
                materialCode = value;
                OnPropertyChanged("MaterialCode2", previewVlaue, materialCode2);
            }
        }

        private short reason2;
        /// <summary>
        /// 物料2NG原因
        /// </summary>
        public short Reason2
        {
            get
            {
                return reason2;
            }
            set
            {
                short previewVlaue = reason2;
                reason2 = value;
                OnPropertyChanged("Reason2", previewVlaue, reason2);
            }
        }

        private string materialCode3;
        /// <summary>
        /// 物料码3
        /// NG车独有
        /// </summary>
        public string MaterialCode3
        {
            get
            {
                return materialCode3;
            }
            set
            {
                string previewVlaue = materialCode3;
                materialCode3 = value;
                OnPropertyChanged("MaterialCode3", previewVlaue, materialCode3);
            }
        }

        private short reason3;
        /// <summary>
        /// 物料3NG原因
        /// </summary>
        public short Reason3
        {
            get
            {
                return reason3;
            }
            set
            {
                short previewVlaue = reason3;
                reason3 = value;
                OnPropertyChanged("Reason3", previewVlaue, reason3);
            }
        }

        private string materialCode4;
        /// <summary>
        /// 物料码4
        /// NG车独有
        /// </summary>
        public string MaterialCode4
        {
            get
            {
                return materialCode4;
            }
            set
            {
                string previewVlaue = materialCode4;
                materialCode4 = value;
                OnPropertyChanged("MaterialCode4", previewVlaue, materialCode4);
            }
        }

        private short reason4;
        /// <summary>
        /// 物料4NG原因
        /// </summary>
        public short Reason4
        {
            get
            {
                return reason4;
            }
            set
            {
                short previewVlaue = reason4;
                reason4 = value;
                OnPropertyChanged("Reason4", previewVlaue, reason4);
            }
        }


        private string materialCode;
        /// <summary>
        /// 物料码
        /// DB7.DBB2
        /// </summary>
        public string MaterialCode
        {
            get
            {
                return materialCode;
            }
            set
            {
                string previewVlaue = materialCode;
                materialCode = value;
                OnPropertyChanged("MaterialCode", previewVlaue, materialCode);
            }
        }

        private bool atStation;
        /// <summary>
        /// 在位
        /// </summary>
        public bool AtStation
        {
            get
            {
                return atStation;
            }
            set
            {
                bool previewVlaue = atStation;
                atStation = value;
                OnPropertyChanged("AtStation", previewVlaue, atStation);
            }
        }

        private bool full;
        /// <summary>
        /// 料车满
        /// </summary>
        public bool Full
        {
            get
            {
                return full;
            }
            set
            {
                bool previewVlaue = full;
                full = value;
                OnPropertyChanged("Full", previewVlaue, full);
            }
        }

        private bool empty;
        /// <summary>
        /// 料车空
        /// </summary>
        public bool Empty
        {
            get
            {
                return empty;
            }
            set
            {
                bool previewVlaue = empty;
                empty = value;
                OnPropertyChanged("Empty", previewVlaue, empty);
            }
        }

        private short workpieceNum;
        /// <summary>
        /// 工件数量
        /// </summary>
        public short WorkpieceNum
        {
            get
            {
                return workpieceNum;
            }
            set
            {
                short previewVlaue = workpieceNum;
                workpieceNum = value;
                OnPropertyChanged("WorkpieceNum", previewVlaue, workpieceNum);
            }
        }
        #endregion
    }
}
